//
//  TutorialViewController2.m
//  TutorialsModule
//
//  Created by Augusto Guido on 13/10/09.
//  Copyright 2009 Moove-IT. All rights reserved.
//

#import "TutorialViewController2.h"
#import "PreviewTutorialView.h"
#import "StepsTableViewController.h"
#import "NewTutorialViewController2.h"
#import "LoginYoutubeViewController.h"


@implementation TutorialViewController2

@synthesize headerView;
@synthesize toolbar;
@synthesize session;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {        
		[self setHidesBottomBarWhenPushed: YES];
    }
    return self;
}

- (IBAction)editTutorial{
	
	if (YES) {  // Si es de tipo steps...
		
	}
	else if (NO) // Si es Video ir al mod de videos...
	{
		
	}	
};

- (IBAction)publishTutorial{
	
	UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:@"Publish" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Youtube", nil] autorelease];
	actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
	[actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
	switch (buttonIndex) {
		case 0:    
				//publicar en facebook
				//inicio la session con los datos de la app	
				// si esta conectado, puiblico
				if(session.isConnected) {
					//[session logout];
					NSLog(@"sigue conectado");
				}
				else
				{
					session = [FBSession sessionForApplication:@API_KEY secret:@API_SECRET delegate:self];
				
					FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:session] autorelease];
					[dialog show];
				}			
				break;
		case 1:
			if (YES) {
				LoginYoutubeViewController *previewView = [[LoginYoutubeViewController alloc] initWithNibName:@"LoginYoutubeViewController" bundle:nil];	
				[self.navigationController pushViewController:previewView animated:YES];
				[previewView release]; 
			}
			break;
		case 2:
			// el boton 2 es el cancel
			
			break;
		default:
			break;
	}
	if (buttonIndex < 2) // el boton 2 es el cancel
	{
		if(buttonIndex == 0) //publicar en facebook 
		{
			//inicio la session con los datos de la app	
			// si esta conectado, puiblico
			if(session.isConnected) {
				//[session logout];
				NSLog(@"sigue conectado");
			}
			else
			{
				session = [FBSession sessionForApplication:@API_KEY secret:@API_SECRET delegate:self];
				FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:session] autorelease];
				[dialog show];
			}			
		}
	}
}


- (IBAction)previewTutorial {
    PreviewTutorialView *previewView = [[PreviewTutorialView alloc] initWithNibName:@"PreviewTutorialView" bundle:nil];	
	[self presentModalViewController:previewView animated:YES];
	[previewView release];
}


- (void)viewDidLoad {
	
	[super viewDidLoad];
	
	UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTutorial)];
	self.navigationItem.rightBarButtonItem = edit;	
	//self.tableView.rowHeight = 200;	
	
	CGRect newFrame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, self.headerView.frame.size.height);
	self.headerView.backgroundColor = [UIColor clearColor];
	self.headerView.frame = newFrame;
	self.tableView.tableHeaderView = self.headerView;	
	
	CGRect newFrame2 = CGRectMake(0.0, 416, self.tableView.bounds.size.width, self.toolbar.frame.size.height);
	self.toolbar.frame = newFrame2;
	[self.tableView addSubview:toolbar];
	
	lblDate.text = @"12/12/2009";
	
	//Si es de tipo Video
	if (YES)
	{
		lbltutorialType.text = @"Video Tutorial";		
	}
	else
	{
		
	}
}
/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	if(indexPath.section == 0)
	{
		cell.textLabel.text = @"View steps";
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	else
	{
		cell.textLabel.text = @"Description";		
	}
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	if(indexPath.section == 0)
	{
		StepsTableViewController *stepsViewController = [[StepsTableViewController alloc] initWithNibName:@"StepsTableViewController" bundle:nil objects:nil];
		[self.navigationController pushViewController:stepsViewController animated:YES];
		[stepsViewController release];
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	if(section == 0) return @"Steps";
	else return @"";
		
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
	if (indexPath.section == 1) return 120;
	else return [tableView rowHeight];
} 

/* FACEBOOK CONNECT*/

-(void)session:(FBSession *)session didLogin:(FBUID)uid
{
}

/**
 * Called when a session is about to log out.
 */
- (void)session:(FBSession*)session willLogout:(FBUID)uid{}

/**
 * Called when a session has logged out.
 */
- (void)sessionDidLogout:(FBSession*)session{}

/* FIN FBC */


- (void)dealloc {
    [super dealloc];
	
}


@end

